package com.xiaodou.mjf.infrastructure.common.app;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import com.xiaodou.mjf.infrastructure.common.entity.CommonEntity;
import com.xiaodou.mjf.infrastructure.common.entity.DataEntity;
import com.xiaodou.mjf.infrastructure.common.entity.IdEntity;

public abstract class JpaSimpleService<T extends CommonEntity,ID extends Serializable> implements CommonService<T,ID> {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	protected abstract JpaRepository<T, ID> getRepository();
	
	@Override
	public T save(T entity){
            return getRepository().save(entity);

	}
	
	@Override
	public void save(Collection<T> entities){
            getRepository().save(entities);
	}

	@Override
	public T get(ID id){
		    return getRepository().findOne(id);
	}

	@Override
	public List<T> list(){
            return getRepository().findAll();
	}

	@Override
	public void delete(ID id){
            T entity = get(id);
            if(entity instanceof DataEntity){
                ((DataEntity) entity).setStatus(DataEntity.DELETE);
                save(entity);
            }else{
                getRepository().delete(id);
            }
	}

	@Override
	public void delete(T entity){
            if(entity instanceof DataEntity){
                ((DataEntity) entity).setStatus(DataEntity.DELETE);
                save(entity);
            }else{
                getRepository().delete(entity);
            }

	}

	@Override
	public boolean exits(ID id){
            return getRepository().exists(id);
	}

    @Override
    public Page<T> page(T entity){
            return getRepository().findAll(preparePage((IdEntity)entity));
    }

    protected PageRequest preparePage(IdEntity entity){

        if(entity.getStart() == 0){
            entity.setStart(1);
        }
        //add by David  2015/4/9 start
        if(entity.getStart()%entity.getLimit()==0){
        	entity.setStart(entity.getStart()+1);
        }
        //add by David  2015/4/9 end
        int page = entity.getStart() / entity.getLimit();
        if(entity.getStart() % entity.getLimit() != 1){
            page += 1;
        }


        PageRequest request = new PageRequest(page,entity.getLimit(), Sort.Direction.ASC,"sort");

        return request;

    }
    
    protected PageRequest preparePage(IdEntity entity,Sort sort){

        if(entity.getStart() == 0){
            entity.setStart(1);
        }
        //add by David  2015/4/9 start
        if(entity.getStart()%entity.getLimit()==0){
        	entity.setStart(entity.getStart()+1);
        }
        //add by David  2015/4/9 end
        int page = entity.getStart() / entity.getLimit();
        if(entity.getStart() % entity.getLimit() != 1){
            page += 1;
        }

        PageRequest request = new PageRequest(page,entity.getLimit(), sort);

        return request;

    }

    @Override
    public Integer count(T entity){
        return 0;
    }
}
